//
//  fractionCalcViewController.m
//  fractionCalc
//
//  Created by panos on 27/5/11.
//  Copyright __MyCompanyName__ 2011. All rights reserved.
//


#import "fractionCalcViewController.h"


@implementation fractionCalcViewController

@synthesize displayString, display;

-(void) viewDidLoad {
	
	firstOperand = YES;
	isNumerator = YES;
	self.displayString = [NSMutableString stringWithCapacity: 40];
	myCalculator = [[Calculator alloc] init];
}   

-(void) processDigit: (int) digit
{
	currentNumber = currentNumber * 10 + digit;
	
	[displayString appendString: [NSString stringWithFormat: @"%i", digit]];
	[display setText: displayString];
}
-(void) processOp: (char) theOp
{
	NSString *opStr;
	
	op = theOp;
	
	switch (theOp) {
		case '+':
			opStr = @" + ";
			break;
		case '-':
			opStr = @" - ";
			break;
		case '*':
			opStr = @" x ";
			break;
		case '/':
			opStr = @" / ";
			break;
	}
	
	[self storeFracPart];
	firstOperand = NO;
	isNumerator = YES;
	
	[displayString appendString: opStr];
	[display setText: displayString];
}
-(void) storeFracPart
{         
	if (firstOperand) {
		if (isNumerator) {
			myCalculator.operand1.numerator = currentNumber;
			myCalculator.operand1.denominator = 1;
		} else
			myCalculator.operand1.denominator = currentNumber;
	} else if (isNumerator) {
		myCalculator.operand2.numerator = currentNumber;
		myCalculator.operand2.denominator = 1;
	} else {
		myCalculator.operand2.denominator = currentNumber;
		firstOperand = YES;
	}
	
	currentNumber = 0;
}

-(IBAction) clickDigit: (id) sender
{
	int digit = [sender tag];
	
	[self processDigit: digit];
}
-(IBAction) clickPlus: (id) sender
{
	[self processOp: '+'];
}
-(IBAction) clickMinus: (id) sender
{
	[self processOp: '-'];
}
-(IBAction) clickMultiply: (id) sender
{
	[self processOp: '*'];
}
-(IBAction) clickDivide: (id) sender
{
	[self processOp: '/'];
}

-(IBAction) clickOver: (id) sender
{
	[self storeFracPart];
	isNumerator = NO;
	[displayString appendString: @"/"];
	[display setText: displayString];
}
-(IBAction) clickEquals: (id) sender
{
	[self storeFracPart];
	[myCalculator performOperation: op];
	[displayString appendString: @" = "];
	[displayString appendString: [myCalculator.accumulator convertToString]];
	[display setText: displayString];
	
	currentNumber = 0;
	isNumerator = YES;
	firstOperand = YES;
	[displayString setString: @""];
}
-(IBAction) clickClear: (id) sender
{   
	isNumerator = YES;
	firstOperand = YES;
	currentNumber = 0;
	[myCalculator clear];
	
	[displayString setString: @""];
	[display setText: displayString];
}

/*
 // The designated initializer. Override to perform setup that is required before the view is loaded.
 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
 if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
 // Custom initialization
 
 }
 return self;
 }
 */

/*
 // Implement loadView to create a view hierarchy programmatically, without using a nib.
 - (void)loadView {   
 }
 */

/*
 // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
 - (void)viewDidLoad {
 [super viewDidLoad];
 }
 */


/*
 // Override to allow orientations other than the default portrait orientation.
 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 // Return YES for supported orientations
 return (interfaceOrientation == UIInterfaceOrientationPortrait);
 }
 */


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}


- (void)dealloc {
    [myCalculator dealloc];
    [super dealloc];
}

@end
